<template>
  <div>
    <el-form class="login-page">
      <h3>系统登录</h3>
      <el-form-item prop="account">
        <el-input type="text" v-model="account" auto-complete="off" placeholder="账号"/>
      </el-form-item>

      <el-form-item prop="password">
        <el-input type="password" v-model="password" auto-complete="off" placeholder="密码"/>
      </el-form-item>

      <el-form-item style="width:100%;">
        <el-button type="primary" style="width:100%;" @click="login">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import store from '@/store'
import {addRoutes} from '../router'

export default {
  data() {
    return {
      account: 'admin',
      password: '123456'
    }
  },
  methods: {
    login() {
      if (!this.account || !this.password) {
        this.$alert('账号或密码不能为空', 'info', {confirmButtonText: 'ok'})
        return
      }

      let jsonParam = {
        account: this.account,
        password: this.password
      }

      var _this = this
      this.$api.userLogin(jsonParam).then(res => {
        if (res.code === '00000') {
          // 先存储再跳转
          store.setUser(res.data)
          // 加载路由
          addRoutes(res.data['menus'])
          // 只能跳转到根路径---别的路径,路由还没有初始化
          _this.$router.push({path: '/'})
        } else {
          _this.$message(res.msg)
          // 删除本地信息
          store.delUser()
        }
      }).catch(e => {
        console.log('接口访问异常!', e)
      })
    }
  }
}
</script>

<style scoped>
.login-page {
  -webkit-border-radius: 5px;
  border-radius: 5px;
  margin: 180px auto;
  width: 350px;
  padding: 35px 35px 15px;
  background: #fff;
  border: 1px solid #eaeaea;
  box-shadow: 0 0 25px #cac6c6;
}
</style>
